﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShareMarking.Systems.Service;
using Common.Utilities.Excel;
using System.Text.RegularExpressions;
using Common.Utilities.Excel.Import;
using System.Data;
using ShareMarking.Service;
using ShareMarking.Entity;
namespace ShareMarking.Web.admin.EnterPrise.QrStore
{
    public partial class storeimport : ManagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        #region 导入体验店信息==================================

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //获取文件路径
            var filepath = Server.MapPath(this.txt_filename.Text.Trim());
            ExcelUtilities util = new ExcelUtilities();
            Excel excel = null;
            //获取配置
            var result = util.ImportExcel(filepath, "storeimport", out excel);
            bool result2 = false;
            bool result3 = false;
            if (result.Result == ExcelResultType.Succeed)
            {
                var updatecount = 0;//成功条数
                int errorCount = 0;//失败条数
                var datasource = excel.DataSet;
                if (datasource.Tables.Count > 0)
                {
                    foreach (DataRow row in datasource.Tables[0].Rows)
                    {
                        #region 循环判断更新或增加
                        if (!row[0].ToString().IsNullOrEmpty())
                        {
                            //var entity = experienceService.Instance.Get_SingleEntity_byWhere(experience._.ShopName == row[3].ToString() && experience._.Province == row[1].ToString() && experience._.City == row[2].ToString());
                            var entity = qr_StoreService.Instance.Get_SingleEntity_byWhere(qr_Store._.StoreCode == row[1].ToString());
                            if (entity != null)
                            {
                                try
                                {
                                    ComLib.LogLib.Log4NetBase.Log("重复的:" + entity.StoreCode);
                                    #region  更新操作 去掉数据源的空格和换行符
                                    //entity.S_Name = Regex.Replace(row[0].ToString().Trim(), @"\r\n", "");
                                    //entity.S_Provinces = Regex.Replace(row[1].ToString().Trim(), @"\r\n", "");
                                    //entity.S_City = Regex.Replace(row[2].ToString().Trim(), @"\r\n", "");
                                    //entity.S_Area = Regex.Replace(row[3].ToString().Trim(), @"\r\n", "");
                                    //entity.S_Region = Regex.Replace(row[4].ToString().Trim(), @"\r\n", "");
                                    //result2 = qr_StoreService.Instance.Update_Entity(entity);
                                    //if (result2)
                                    //{
                                    //    updatecount += 1;
                                    //}
                                    //else
                                    //{
                                    //    errorCount += 1;//已经添加
                                    //}

                                    #endregion

                                    errorCount += 1;//已经添加
                                }
                                catch (Exception ex)
                                {
                                    AddAdminLog(DTEnums.ActionEnum.Import.ToString(), "导入qr_Store出错 错误为" + ex.Message + "");
                                    JscriptMsg("导入更新数据失败！请联系管理员！", "Index.aspx", "Error");
                                }
                            }
                            else
                            {

                                #region  增加操作 去掉数据源的空格和换行符
                                try
                                {
                                    qr_Store store = new qr_Store();
                                    store.S_Name = Regex.Replace(row.ItemArray[0].ToString().Trim(), @"\r\n", "");
                                    store.StoreCode = Regex.Replace(row.ItemArray[1].ToString().Trim(), @"\r\n", "");
                                    store.S_Provinces = Regex.Replace(row.ItemArray[2].ToString().Trim(), @"\r\n", "");
                                    store.S_City = Regex.Replace(row.ItemArray[3].ToString().Trim(), @"\r\n", "");
                                    store.S_Area = Regex.Replace(row.ItemArray[4].ToString().Trim(), @"\r\n", "");
                                    store.S_Region = Regex.Replace(row.ItemArray[5].ToString().Trim(), @"\r\n", "");
                                    store.CreateOn =DateTime.Now;
                                    store.ModifiedOn = DateTime.Now;
                                    store.isneedupdate = true;
                                    store.isneedupload = true;
                                    result2 = qr_StoreService.Instance.Add_Entity(store);
                                    if (result2)
                                    {
                                        updatecount += 1;
                                    }
                                    else
                                    {
                                        errorCount += 1;//已经添加
                                    }
                                }
                                catch (Exception ex)
                                {
                                    AddAdminLog(DTEnums.ActionEnum.Import.ToString(), "导入qr_Store出错 错误为" + ex.Message + "");
                                    JscriptMsg("导入添加数据失败！请联系管理员！", "Index.aspx", "Error");
                                }

                                #endregion
                            }
                        }
                        else
                        {
                            errorCount += 1;//失败条数
                        }
                        #endregion
                    }
                }
                JscriptMsg("导入成功" + updatecount + "条，失败" + errorCount + "条！", "Index.aspx", "Success");
            }
            else
            {
                JscriptMsg("addressimport", "导入失败！请检查excel！", "", "Error");
            }
        }
        #endregion
    }
}